﻿using DataLayer;
using SupportLayer.Constantes.Outros;
using SupportLayer.Enums;
using SupportLayer.Objetos.Usuario;
using SupportLayer.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TranSys_Web.TranSys
{
  public partial class confirmMail : System.Web.UI.Page
  {
    public string GetAuth
    {
      get
      {
        if (Request.QueryString[OUTROS.AUTH] == null ||
          Request.QueryString[OUTROS.AUTH].ToString().Equals(string.Empty))
          return "";

        return Request.QueryString[OUTROS.AUTH].ToString();
      }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
      try
      {
        if (GetAuth.Equals(string.Empty))
          Response.Redirect("login.aspx?" + OUTROS.CODIGO_ERRO + "=" + ((int)CodigoErro.LinkInvalido).ToString());

        string decrypt = Criptografia.Decriptar(OUTROS.CHAVE, OUTROS.VETOR, GetAuth);

        int ID = Convert.ToInt32(decrypt.Substring(0, decrypt.IndexOf("_TRANSYS_")));
        DateTime data = Convert.ToDateTime(decrypt.Substring(decrypt.IndexOf("_TRANSYS_") + 9));



        foreach (UsuarioClass Usuario in new AcessoBanco().GetNotConfirmedMailUsers())
        {
          if (Usuario.ID == ID)
          {
            int hr = data.Subtract(Usuario.DataCadastro).Days;

            if (hr > 7)
              Response.Redirect("login.aspx?" + OUTROS.CODIGO_ERRO + "=" + ((int)CodigoErro.LinkExpirado).ToString());

            new AcessoBanco().UpdateMailUsuario(Usuario);
            Session[OUTROS.USUARIO_ID] = Usuario.ID;
            Response.Redirect("createPlaca.aspx", false);
          }
        }
      }
      catch (Exception ex)
      {
        string et = ex.Message;
        Response.Redirect("login.aspx?" + OUTROS.CODIGO_ERRO + "=" + ((int)CodigoErro.LinkInvalido).ToString());
      }
    }
  }
}